//
//  ZYHitTestView.m
//  OC_Principle
//
//  Created by wzp on 2025/5/17.
//

#import "ZYHitTestView.h"

@implementation ZYHitTestView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.hidden == YES || self.userInteractionEnabled == NO) {
        return nil;
    }
    
    if ([self pointInside:point withEvent:event]) {
        for (UIView *subView in self.subviews.reverseObjectEnumerator) {
            CGPoint newPoint = [self convertPoint:point toView:subView];
            UIView *hitView = [subView hitTest:newPoint withEvent:event];
            if (hitView) {
                return hitView;
            }
        }
        return self;
    }
    return nil;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGFloat r = 20;
    CGFloat x = self.center.x;
    CGFloat y = self.center.y;
    
    if ((point.x - x) * (point.x - x) + (point.y - y) * (point.y - y) <= r) {
        return YES;
    }
    return NO;
}

@end
